<template>
  <div>
    <!-- 导航 -->
    <van-nav-bar
      title="更改名字"
      left-text="取消"
      right-text="保存"
      left-arrow
      @click-left="onClickLeft"
      @click-right="onClickRight"
    />

    <!-- 文本 -->
    <van-field
      v-model="nickname"
      rows="2"
      autosize
      type="textarea"
      maxlength="11"
      placeholder="请输入新的名字"
      show-word-limit
    />
  </div>
</template>

<script>
import {editUserProfileAPI} from '@/api'

export default {
   data() {
      return {
         nickname: this.name,
      }
   },

   props: {
      name: {
         type: String,
        required: true
     } 
   },

   methods: {
      onClickLeft() {
         this.$emit('close')
      },
      
      async onClickRight() {
         // console.log('99');
         this.$toast.loading({
            message: '更新中',
            forbidClick:true
         })

         //1.发送请求 更新上线
         await editUserProfileAPI({
            name:this.nickname
         })
         //2.手动更新视图
         //sync:v-bind指令修饰符

         //用法 属性.sync = 'xxx'  作用 更新绑定的值 
         //
         this.$emit('update:name', this.nickname)

         //3.关闭弹窗
         this.$emit('close')

         //4 成功
         this.$toast.success('更新成功')
      },
   }
};
</script>

<style>
</style>